package home

import (
	// "fmt"

	"gitee.com/chenrh/finego/app"
	"gitee.com/chenrh/finego/backends/crawler"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// NewsController 资讯控制器
type NewsController struct {
	// Optionally: context is auto-binded by Iris on each request,
	// remember that on each incoming request iris creates a new UserController each time,
	// so all fields are request-scoped by-default, only dependency injection is able to set
	// custom fields like the Service which is the same for all requests (static binding).
	Ctx iris.Context
}

// BeforeActivation called once, before the controller adapted to the main application
// and of course before the server ran.
// After version 9 you can also add custom routes for a specific controller's methods.
// Here you can register custom method's handlers
// use the standard router with `ca.Router` to do something that you can do without mvc as well,
// and add dependencies that will be binded to a controller's fields or method function's input arguments.
func (c *NewsController) BeforeActivation(b mvc.BeforeActivation) {
	// b.Handle("GET", "/about-us", "AboutUs")
	// b.Handle("GET", "/resume", "MyResume")
	// b.Handle("GET", "/home", "Dashboard")
}

// GetNews 首页
func (c *NewsController) GetNews() mvc.Result {

	neteaseCrawler := crawler.NetEaseCrawler{}

	newsresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelNews)
	yuleresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelEntertainment)
	tiyuresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelSports)
	caijresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelFinance)
	junsresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelMilitary)
	techresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelTech)
	shouresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelPhone)
	digiresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelDigital)
	shisresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelFation)
	gameresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelGame)
	educresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelEducation)
	jianresult, err := neteaseCrawler.StartCrawler(crawler.NetEaseChannelHealth)

	if err != nil {
		app.Logger().Errorf("爬取数据时，遇到问题： %s", err)
	}

	// log.Println(newsresult)
	return mvc.View{
		Name: "news.html",
		Data: iris.Map{
			"Title":         "新闻资讯",
			"News":          newsresult,
			"Entertainment": yuleresult,
			"Sports":        tiyuresult,
			"Finance":       caijresult,
			"Military":      junsresult,
			"Tech":          techresult,
			"Phone":         shouresult,
			"Digital":       digiresult,
			"Fation":        shisresult,
			"Game":          gameresult,
			"Education":     educresult,
			"Health":        jianresult,
		},
	}
}
